package demo.practice.dp;

public class P256 {

    public static void main(String[] args) {
        P256 p256 = new P256();
        p256.minCost(new int[][]{{17, 2, 17}, {16, 16, 5}, {14, 3, 19}});
    }

    public int minCost(int[][] costs) {
        if(costs==null||costs.length==0){
            return  0;
        }
        //dp[i][0],dp[i][1],dp[i][3] 分别代表 第i个房子刷 红色、蓝色或者绿色 花费最少钱数
        int[][] dp = new int[costs.length + 1][3];
        dp[0][0] = 0;
        dp[0][1] = 0;
        dp[0][2] = 0;


        for (int i = 1; i <= costs.length; i++) {
            for (int j = 0; j < 3; j++) {//代表 dp[i][j]
                dp[i][j] = Integer.MAX_VALUE;
                for (int k = 0; k< 3; k++) {   //代表 dp[i-1][k]
                    //撞色了
                    if (j == k)
                        continue;
                    dp[i][j] = Math.min(dp[i][j], dp[i - 1][k] + costs[i - 1][j]);
                }
            }
        }

        return Math.min(Math.min(dp[costs.length][0], dp[costs.length][1]), dp[costs.length][2]);
    }
}
